#!/usr/bin/env bash

#服务器系统配置初始化脚本
dns_ok=0
timeZone_ok=0
time_ok=0
ulimit_ok=0
softwareArray=(wget curl ifconfig vim htop)
################## 检查 ###################
#dns检查
function check_dns() {
  echo -n "DNS解析检查: "
  ping -c 2 www.baidu.com >/dev/null
  if [ $? -eq 0 ]; then
    echo_green "DNS解析正常!"
    dns_ok=1
  else
    echo_red "DNS解析异常,无法Ping通www.baidu.com!"
    dns_ok=2
  fi
}

# 时间时区检查
function check_timeZone() {
  echo -n "时区检查: "
  date -R | grep +0800 >/dev/null
  if [ $? -eq 0 ]; then
    echo_green "当前时区为东八区!"
    timeZone_ok=1
  else
    echo_red "当前时区非东八区!"
    timeZone_ok=2
  fi

  echo -n "网络时间: "
  # 获取网络时间
  local ntpTime=$(curl -s http://quan.suning.com/getSysTime.do)
  local localTime=$(date "+%F %T")
  local flag=0
  if [ ! -f "${ntpTime}" ]; then
    ntpTime="${ntpTime#*:}"
    ntpTime="${ntpTime%%,*}"
    ntpTime="${ntpTime//\"/}"
    echo_green "${ntpTime}"
    flag=1
  else
    echo_red "[error]获取网络时间失败,请检查网址是否正常: quan.suning.com/getSysTime.do "
  fi

  echo -n "系统时间: "
  local time_diff=$(($(date +%s -d "$ntpTime") - $(date +%s -d "$localTime")))
  local time_diff="${time_diff#-}"

  if [[ "${time_diff}" == "0" && ${flag} == 1 ]] || [[ "${time_diff}" < "2" && ${flag} == 1 ]]; then
    echo_green "$(date "+%F %T")"
    time_ok=1
  else
    echo_red "$(date "+%F %T") 系统时间与网络时间不一致!"
    time_ok=2
  fi

}

# 最大打开文件数检查
function check_ulimit() {
  echo -n "最大打开文件数检查: "
  local number=$(ulimit -n)
  if [[ $? -eq 0 && ${number} -gt 2048 ]]; then
    echo_green "${number}"
    ulimit_ok=1
  else
    echo_red "${number} 数量过低,访问量多高并发的情况下可能有问题!"
    ulimit_ok=2
  fi
}

# 常用软件检查
function check_software() {
  echo -n "常用软件检查: ${softwareArray[@]}"
  for ((i = 0; i < ${#softwareArray[@]}; i++)); do
    if [[ "" == $(command -v ${softwareArray[i]}) ]]; then
      echo_red "检测到 ${softwareArray[i]} 未安装"
    fi
  done
}

################## 优化 ###################
#dns修改
function dns_update() {

  local nameserver="114.114.114.114"
  local nameserver1="8.8.8.8"

  cp /etc/resolv.conf /etc/resolv.conf.backup

  echo "nameserver ${nameserver}" >/etc/resolv.conf
  echo "nameserver ${nameserver1}" >/etc/resolv.conf

  echo "dns解析修改为:${nameserver} ${nameserver1}"

}
#时区修改
function timeZone_update() {
  sudo rm -f /etc/localtime >/dev/null
  ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime >/dev/null
  if [ $? -eq 0 ]; then
    echo "时区修改为:Asia/Shanghai"
  else
    echo_red "时区修改失败!"
  fi

}
#时间同步修改
function time_update() {
  if [[ "" == $(command -v ntpdate) ]]; then
    echo "ntpdate命令不存在,开始安装!"
    install_soft ntpdate
  fi

  ntpdate ntp.aliyun.com >/dev/null

  if [ $? -eq 0 ]; then
    echo "时间同步服务器为: ntp.aliyun.com"
  else
    echo_red "时间同步修改失败!"
  fi
}

#最大打开文件数修改
function ulimit_update() {
  local limit=65535
  ulimit -n 65535
  sleep 1
  echo "* soft nofile ${limit}" >>/etc/security/limits.conf
  echo "* hard nofile ${limit}" >>/etc/security/limits.conf
  echo "* soft nproc ${limit}" >>/etc/security/limits.conf
  echo "* hard nproc ${limit}" >>/etc/security/limits.conf
  echo "root soft nofile ${limit}" >>/etc/security/limits.conf
  echo "root hard nofile ${limit}" >>/etc/security/limits.conf
  echo "最大打开文件数修改为: ${limit}"

}

# 常用软件安装
function software_update() {
  for ((i = 0; i < ${#softwareArray[@]}; i++)); do
    if [[ "" == $(command -v ${softwareArray[i]}) ]]; then
      if [[ "ifconfig" == "${softwareArray[i]}" ]]; then
        install_soft "net-tools"
      else
        install_soft "${softwareArray[i]}"
      fi
      echo "安装软件:${softwareArray[i]}"
    fi
  done
}

function manual_optimization() {
  clear
  echo -e "=========================LINUX_TOOLS=========================="
  echo -e "(1) DnsUpdate(dns优化)                  (2) TimeZoneUpdate(时区优化)"
  echo -e "(3) TimeUpdate(时间同步优化)             (4) UlimitUpdate(最大打开文件数修改)     "
  echo -e "(0) Back(返回上一层菜单)"
  echo -e "=========================LINUX_TOOLS=========================="

  read -r -p "请输入要安装服务的编号(select)：" input
  case $input in
  1)
    dns_update
    ;;
  2)
    timeZone_update
    ;;
  3)
    time_update
    ;;
  4)
    ulimit_update
    ;;
  0)
    main
    ;;
  *)
    clear
    echo_red "请输入正确的编号，ctrl+c退出"
    sleep 1s
    system_main
    ;;
  esac
}

function optimization() {
  echo -e "\n#####################################"
  if [[ ${dns_ok} == 1 && ${timeZone_ok} == 1 && ${time_ok} == 1 && ${ulimit_ok} == 1 ]]; then
    echo_green "未发现需要优化的项目!"
  else
    read -r -p "是否自动优化标红项目,自动优化Yy|手动优化Nn|放弃优化0: " input
    case $input in
    [yY][eE][sS] | [yY])
      if [[ ${dns_ok} == 2 ]]; then dns_update; fi           # dns优化
      if [[ ${timeZone_ok} == 2 ]]; then timeZone_update; fi # 时区优化
      if [[ ${time_ok} == 2 ]]; then time_update; fi         # 时间同步优化
      if [[ ${ulimit_ok} == 2 ]]; then ulimit_update; fi     # 最大打开文件数修改
      software_update                                        # 常用软件安装
      ;;
    [nN])
      manual_optimization
      ;;
    [0][oO] | [0])
      echo ""
      ;;

    *) ;;

    esac
  fi

}

function system_init() {
  # ###############检查###############
  check_dns      #dns检查
  check_timeZone #时区检查
  check_ulimit   #最大打开文件数检查
  check_software #常用软件检查
  optimization   #优化

  echo "执行完毕!"
}
